define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'mall/user/address/index',
                    edit_url: 'mall/user/address/edit',
                    del_url: 'mall/user/address/del',
                    table: 'mall_user_address',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {field: 'id', title: __('ID')},
                        {field: 'user_id_text', title: __('User_id')},
                        {field: 'name', title: __('Username')},
                        {field: 'phone', title: __('Phone')},
                        {field: 'area_ids_text', title: __('Area'), align:"left"},
                        {field: 'address', title: __('Useraddress'), align:"left"},
                        {field: 'isDefault', title: __('Isdefault'), formatter: Controller.api.formatter.toggle},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        	$("#c-category1").trigger("fa.event.areaidsupdated",'edit');
    	},
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                //四级地区联动参数传递
                $("#c-category2").data("params", function (obj) {
                    return {custom: {pid: $("#c-category1").val()}};
                });
                $("#c-category3").data("params", function (obj) {
                    return {custom: {pid: $("#c-category2").val()}};
                });
                $("#c-category4").data("params", function (obj) {
                    return {custom: {pid: $("#c-category3").val()}};
                });
                //一级地区选定，清除二、三、四级选项
                $(document).on("change", "#c-category1", function () {
                	require(['selectpage'], function () {
                		$('#c-category2').selectPageClear();
                		$('#c-category3').selectPageClear();
                		$('#c-category4').selectPageClear();
                	});
                });
                //二级地区选定，清除三、四级选项
                $(document).on("change", "#c-category2", function () {
                	require(['selectpage'], function () {
                		$('#c-category3').selectPageClear();
                		$('#c-category4').selectPageClear();
                	});
                });
                //三级地区选定，清除四级选项
                $(document).on("change", "#c-category3", function () {
                	require(['selectpage'], function () {
                		$('#c-category4').selectPageClear();
                	});
                });
                //拼接四级地区选项值，用于保存
                $(document).on("change", "#c-category4", function () {
            		$("#c-area_ids").val( $("#c-category1").val()+","+$("#c-category2").val()+","+$("#c-category3").val()+","+$(this).val());
                });
                //四级地区默认值设置（只执行一次）
                $(document).one("fa.event.areaidsupdated", "#c-category1", function () {
                	var a = $("#c-area_ids").val().split(",");
            		$("#c-category1").val(a[0]);
            		$("#c-category2").val(a[1]);
            		$("#c-category3").val(a[2]);
            		$("#c-category4").val(a[3]);
                });
            },
            formatter: {
                //去除点击事件的开关
                toggle: function (value, row, index) {
                    var color = typeof this.color !== 'undefined' ? this.color : 'success';
                    var yes = typeof this.yes !== 'undefined' ? this.yes : 1;
                    var no = typeof this.no !== 'undefined' ? this.no : 0;
                    return "<a href='javascript:;' data-toggle='tooltip' data-id='" + row.id + "'  data-params='"
                        + this.field + "=" + (value == yes ? no : yes) + "'><i class='fa fa-toggle-on " + (value == yes ? 'text-' + color : 'fa-flip-horizontal text-gray') + " fa-2x'></i></a>";
                },

            }
        }
    };
    return Controller;
});